package mmi.file;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.Vector;

import mmi.utils.Action;
import mmi.utils.QuestionAnswer;

/**
 * WriteFile contains some functions to write to (XML) files.
 * @see ReadFile
 * 
 * @author louis
 *
 */
public class WriteFile {

	private Vector<Action> actionList;
	private Vector<QuestionAnswer> questionAnswerList;

	private File file;

	public WriteFile(File file, Vector<Action> actionList, Vector<QuestionAnswer> questionAnswerList) {
		this.actionList = actionList;
		this.questionAnswerList = questionAnswerList;
		this.file = file;

		try {
			writeActions();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void writeActions() throws IOException
	{
		Writer output = null;

		String s = "";

		s +="<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n";
		s += "<Actions>\r\n";

		for(Action a : actionList)
		{
			s += a.getXMLRepresentation() + "\r\n";
		}

		s += "</Actions>\r\n";

		s += "<Questionnaire>\r\n";
		for(QuestionAnswer qa : questionAnswerList)
		{
			s += "\r\t<QA question=\"" + qa.getQuestion() + "\">" + qa.getAnswer() + "</QA>\r\n";
		}

		s += "</Questionnaire>";

		output = new BufferedWriter(new FileWriter(file.getAbsolutePath()+".txt"));
		output.write(s);
		output.close();
	}
}
